/*
 * @lc app=leetcode.cn id=599 lang=javascript
 *
 * [599] 两个列表的最小索引总和
 */

// @lc code=start
/**
 * @param {string[]} list1
 * @param {string[]} list2
 * @return {string[]}
 */
var findRestaurant = function(list1, list2) {
  let map = new Map()
  let res = []
  let min = Infinity
  for (let i = 0; i < list1.length; i++) {
    map.set(list1[i], i)
  }

  for (let j = 0; j < list2.length; j++) {
    let iTotal = 0
    let item = list2[j]
    let i = map.get(item)
    if (i !== undefined) {
      iTotal = i + j
      if (min === iTotal) {
        res.push(item)
        min = iTotal
      }
      if (iTotal < min) {
        res = [item]
        min = iTotal
      }
    }
  }
  return res
};
// @lc code=end

